import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import org.diyse.gateway.pubsub.xmpp.internal.data.Data;
import org.diyse.gateway.pubsub.xmpp.internal.data.Item;

/*
 * N. Dumont, TAI Lab
 *
 * Class DataTester created on 20 mai 2011
 *
 * Copyright (c) 2009 Thales Communications
 * 160 bd de Valmy - BP82 92704 COLOMBES cedex - France
 * All rights reserved.
 *
 */

/**
 * @author tai
 * 
 */
public class DataTester {
	public static void main(String[] args) {
		testUnmarshall();
		
		Item item = new Item();
		item.setId("id1");
		
		Data data = new Data();
		data.setType("double");
		data.setValue("1.5");
		
		item.setData(data);
		
		saveAddedDeviceDescription(item, "org.diyse.gateway.pubsub.xmpp.internal.data", "toto.xml");

	}
	
	public static void saveAddedDeviceDescription(Object device,String namespace, String file) {
		try {
			JAXBContext l_context = JAXBContext.newInstance(namespace);

			// Unmarshaller u = l_context.createUnmarshaller();
			// Object o = u.unmarshal(new File(FILE_LOCATION));
			//
			// Devices l_devices = (Devices) o;
			//			
			// // remove old device if duplicated
			// for (Device i_device : l_devices.getDevice()) {
			// if (i_device.getId().equals(device.getId())) {
			// l_devices.getDevice().remove(i_device);
			// break;
			// }
			// }
			//			
			// l_devices.getDevice().add(device);

			Marshaller l_marshaller = l_context.createMarshaller();

			OutputStream os = new FileOutputStream(file);
			l_marshaller.marshal(device, os);

			os.close();

			System.out.println("Marshalling done.");

		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	private static void testUnmarshall(){
		JAXBContext l_context = null;
		try {
			l_context = JAXBContext.newInstance("org.diyse.gateway.pubsub.xmpp.internal.data");
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String data = "<item id='ll5nwS9500g34vJ'><data xmlns=\"http://jabber.org/protocol/pubsub\" type=\"int\" value=\"15\"/></item>";
		ByteArrayInputStream input = new ByteArrayInputStream (data.getBytes()); 
		
		Unmarshaller unmarshaller;
		try {
			unmarshaller = l_context.createUnmarshaller();
			Object o = unmarshaller.unmarshal(input);
			Item item = (Item)o;
			System.out.println(item.getData().toString());
		} catch (JAXBException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println("Finish !");
		
	}
}
